package com.rec.controller;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.rec.entity.Students;
import com.rec.service.StudentsService;
import com.rec.utils.ExportBeanExcel;

@RestController
@RequestMapping("stu")
@CrossOrigin
public class StudentController {
	@Autowired
	private StudentsService s;
	/**
	 * private String cardno; 学号：精准查询
		private String name;姓名：模糊查询
		private String sex;性别：精准查询
		private Integer memberid;院系专业：精准查询
		private Integer status;
	 * @return
	 */
	@RequestMapping("getAll")
	public Map<String,Object> getBySearch(String cardno,String name,String sex,
			Integer memberid,Integer status,Integer page, Integer limit) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("cardno", cardno);
		params.put("name", name);
		params.put("sex", sex);
		params.put("memberid", memberid);
		params.put("status", status);
		limit = limit == null ? 10 : limit;
		page = page == null ? 0 : page - 1;
		params.put("limit", limit);
		params.put("page", page);
		return s.all(params);
	}
	/**
	 *String cardno; 学号：String name;姓名 String sex;性别： Integer memberid;院系专业 Integer status状态;
	 * @return
	 */
	@RequestMapping("add")
	public Map<String,Object> add(String cardno,String name,String sex, Integer memberid,Integer status,Students students){
		Map<String, Object> params = new HashMap<String, Object>();
		students.setId(null);
		students.setCardno("s"+cardno);
		students.setName(name);
		students.setSex(sex);
		students.setMemberid(memberid);
		students.setStatus(status);
		params.put("Stu", students);
		return s.add(params);
	}
	/**
	 * 学号不可修改
	 * @param id
	 * @param name
	 * @param sex
	 * @param memberid
	 * @param status
	 * @param students
	 * @return
	 */
	@RequestMapping("update")
	public Map<String,Object> update(Integer id,String name,String sex, Integer memberid,Integer status){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		params.put("name", name);
		params.put("sex", sex);
		params.put("memberid",memberid);
		params.put("status",status);
		return s.update(params);
	}
	@RequestMapping("delete")
	public Map<String,Object> delete(Integer id){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("delid", id);
		return s.delete(params);
	}
	
	@RequestMapping("puton")
	public Map<String,Object> putOnExcel(@RequestParam(value = "file",required = false)MultipartFile file) {
		System.out.println("这里是测试"+file);
		Map<String, Object> params = new HashMap<String, Object>();
		int num = 0;	//返回值
		int f = 0;//记录重复数据
		// 读取excel表格
		try {
			List<List<String>> lists;
			lists = ExportBeanExcel.readExcel(file.getInputStream());
			System.out.println(lists);
			// 判断集合是否为空
			if (!CollectionUtils.isEmpty(lists)) {
				for (int i = 1; i < lists.size(); i++) {
					List<String> list = lists.get(i);
					Double scardno = Double.parseDouble(list.get(0));
					Integer car = new Double(scardno).intValue();
					String cardno = car.toString();
					String name = list.get(1);
					String sex = list.get(2);
					Double doumeid = Double.parseDouble(list.get(3));
					Integer memberid = new Double(doumeid).intValue();
					Double dousstatus = Double.parseDouble(list.get(4));
					Integer status = new Double(dousstatus).intValue();
					Students stu = new Students(null, "s"+cardno, name, sex, memberid, status,null);
					try {
						//判断卡号是否存在
						if(s.findCardnoExist("s"+cardno)==null) {
							num+=1;//成功导入的
							s.addStudent(stu);
						}else {
							f+=1;//未成功的
						}
					} catch (Exception e) {
						e.printStackTrace();
						num = 0;
					}
				}//for循环结束
				params.put("f", f);
				params.put("num", num);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return params;
	}
	//批量下载
	@RequestMapping("down")
	public Integer down(@RequestParam("sid")List<Integer> sid) throws IOException {
		return s.download(sid);
	}
	
	
}
